﻿/*********************************************
Copyright (c) 2013 All Right Reserved, bixaps

This source is subject to the Microsoft Permissive License.
Please see the License.txt file for more information.
All other rights reserved.      

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Author: Raj Bandi
Email: raj.bandi@hotmail.com

Util helper classes 
*********************************************/
(function () {
    "use strict";

    WinJS.Namespace.define("Bixaps.Util",
        {
            Icons: {
                ARROW_HEAD_RIGHT: ''
               , ARROW_HEAD_DOWN: ''
               , ARROW_HEAD_LEFT: ''
               , ARROW_HEAD_UP: ''
            },
            Keys: {
                BACKSPACE: 8,
                COMMA: 188,
                DELETE: 46,
                DOWN: 40,
                END: 35,
                ENTER: 13,
                ESCAPE: 27,
                HOME: 36,
                LEFT: 37,
                NUMPAD_ADD: 107,
                NUMPAD_DECIMAL: 110,
                NUMPAD_DIVIDE: 111,
                NUMPAD_ENTER: 108,
                NUMPAD_MULTIPLY: 106,
                NUMPAD_SUBTRACT: 109,
                PAGE_DOWN: 34,
                PAGE_UP: 33,
                PERIOD: 190,
                RIGHT: 39,
                SPACE: 32,
                TAB: 9,
                UP: 38
            },
            IsNull: function (el) {
                return el == null || el == 'undefined';
            },
            IsNullOrEmpty: function (el) {
                return el == null || el == 'undefined' || el == '';
            },
            getListItemHeight: function (el) {
                var h = 0;
                if (!Bixaps.Util.IsNull(el)) {
                    var node = null;
                    for (var i = 0; i < el.childNodes.length; i++) {
                        node = el.childNodes[i];
                        if (node.innerHTML.length > 5) {
                            h += el.childNodes[i].offsetHeight;
                        }
                    }
                }
                return h;
            }
        });

})();


(function () {
    "use strict";

    var listView = WinJS.UI.ListView;

    //define the events required
    listView.onItemsLoading = "onItemsLoading";
    listView.onItemsLoaded = "onItemsLoaded";
    listView.onViewPortLoaded = "onViewPortLoaded";
    listView.onItemFocus = "onItemFocus";
    listView.onItemBlur = "onItemBlur";


    listView.prototype._setViewStateOld = listView.prototype._setViewState;
    listView.prototype._setViewState = function (state, detail) {
        this._setViewStateOld(state, detail);
        var eventObject;
        switch (state) {
            case this._LoadingState.viewPortLoaded:
                eventObject = document.createEvent("CustomEvent");
                eventObject.initCustomEvent(WinJS.UI.ListView.onViewPortLoaded, true, false, detail);
                this._element.dispatchEvent(eventObject);
                break;
            case this._LoadingState.itemsLoading:
                eventObject = document.createEvent("CustomEvent");
                eventObject.initCustomEvent(WinJS.UI.ListView.onItemsLoading, true, false, detail);
                this._element.dispatchEvent(eventObject);
                break;
            case this._LoadingState.complete:
                eventObject = document.createEvent("CustomEvent");
                eventObject.initCustomEvent(WinJS.UI.ListView.onItemsLoaded, true, false, detail);
                this._element.dispatchEvent(eventObject);
                break;
        }
    };

    listView.prototype._onFocusOld = listView.prototype._onFocus;
    listView.prototype._onFocus = function (event) {
        this._onFocusOld(event);
        var eventObject = document.createEvent("CustomEvent");
        eventObject.initCustomEvent(WinJS.UI.ListView.onItemFocus, true, false, event);
        this._element.dispatchEvent(eventObject);
    };

    listView.prototype._onBlurOld = listView.prototype._onBlur;
    listView.prototype._onBlur = function (event) {
        this._onBlurOld(event);
        var eventObject = document.createEvent("CustomEvent");
        eventObject.initCustomEvent(WinJS.UI.ListView.onItemBlur, true, false, event);
        this._element.dispatchEvent(eventObject);
    };

    listView.prototype.adjustExpanderItems = function () {
        var containers = new WinJS.Utilities.QueryCollection(this.element.querySelectorAll(".win-container"));
        var listView = this.element;
        if (containers.length > 0) {
            var top = 5;
            containers.forEach(function (container) {
                var listItem = container.querySelector(".win-item");
                if (listItem) {

                    var expanders = new WinJS.Utilities.QueryCollection(listItem.querySelectorAll(".win-expander"));
                    expanders.forEach(function (expander) {
                        expander.winControl.parentListView = listView;

                    }); //&& firstExpander == null
                    if (expanders[0]) {
                        expanders[0].winControl._adjustDimensions();
                    }
                }

                var h = Bixaps.Util.getListItemHeight(container);
                if (h > 0) {
                    container.style.top = top + "px";
                    container.style.height = h + "px";
                    top = top + h + 10;
                }

            });
            var h = Bixaps.Util.getListItemHeight(containers[0].parentNode);
            containers[0].parentNode.style.height = h + "px";
            var surface = this.element.querySelector(".win-surface");
            surface.style.height = h + "px";
            var backdrop = this.element.querySelector(".win-backdrop");
            backdrop.style.top = h + "px";
        }


    };

    WinJS.Class.mix(WinJS.UI.ListView,
        WinJS.Utilities.createEventProperties(

        WinJS.UI.ListView.onViewPortLoaded,
        WinJS.UI.ListView.onItemsLoading,
        WinJS.UI.ListView.onItemsLoaded,
        WinJS.UI.ListView.onItemFocus,
        WinJS.UI.ListView.onItemBlur

    ));

})();

(function () {

    "use strict";

    WinJS.Namespace.define("Bixaps.UI", {

        processAll: function () {

            Bixaps.UI.processExpanders();
        }
    });

})();


(function () {

    "use strict";

    WinJS.Namespace.define("Bixaps.UI.Animation", {

        slide: WinJS.Utilities.markSupportedForProcessing(function (elem, o) {
            var options = o || {};
            var t = {};
            var oldMargins = { top: elem.style.marginTop, left: elem.style.marginRight };
            var wrapper = document.createElement('div');
            if (options.wrapperStyle)
                wrapper.setAttribute('style', options.wrapperStyle);

            wrapper.style.overflow = 'hidden';
            elem.parentNode.replaceChild(wrapper, elem);
            wrapper.appendChild(elem);
            var mode = options.mode || 'show';
            elem.style.display = '';

            var ew = elem.offsetWidth;
            var eh = elem.offsetHeight;
            var dir = options.direction || 'down';
            if (dir === 'left' || dir === 'right') {
                if (elem.offsetWidth == 0)
                    elem.style.width = 'auto';
                t.property = 'margin-left';
                t.from = options.from || (dir == 'left') ? "0px" : (ew * -1) + "px";
                t.to = options.to || ((dir === 'left') ? (ew * -1) + "px" : '0px');
            } else {
                if (elem.offsetHeight == 0)
                    elem.style.height = 'auto';
                t.property = 'margin-top';
                t.from = options.from || (dir == 'up') ? "0px" : (eh * -1) + "px";
                t.to = options.to || ((dir === 'up') ? (eh * -1) + "px" : '0px');
            }

            t.delay = options.delay || 0;
            t.timing = options.timing || 'cubic-bezier(0.1, 0.9, 0.2, 1)';
            t.duration = options.duration || 400;

            var promise1 = WinJS.UI.executeTransition(elem, t).then(function () {
                var fragment = document.createDocumentFragment();
                while (wrapper.firstChild) {
                    fragment.appendChild(wrapper.firstChild);
                }
                wrapper.parentNode.replaceChild(fragment, wrapper);
                elem.style.marginTop = oldMargins.top;
                elem.style.marginLeft = oldMargins.left;
                elem.style.display = (mode === 'hide') ? 'none' : '';
            });

            return WinJS.Promise.join(promise1);
        })

    });

})();
